package elicecn.util.encrypt;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

import elicecn.util.DigitalUtils;

/**
 * 可用于计算数据流的 CRC-32 的类。
 * 
 * @author 屹砾
 * @email eli.wuhan@gmail.com
 * @time 2009-3-1
 */
public class CRC32 extends EncryptBase {
	private static final long serialVersionUID = -3779195686945168908L;
	private java.util.zip.CRC32 crc = new java.util.zip.CRC32();

	protected String calculate(InputStream is) throws IOException {
		int idx = 0;
		long result = 0L;
		BufferedInputStream bis = new BufferedInputStream(is, BUFFER_SIZE);
		byte[] bt = new byte[BUFFER_SIZE];
		synchronized (crc) {
			crc.reset();
			while ((idx = bis.read(bt)) != -1) {
				crc.update(bt, 0, idx);
			}
			result = crc.getValue();
		}
		is.close();
		bis.close();
		return new String(DigitalUtils.toHex(result)).substring(8);
	}
}
